home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Common / CXBindCtx.cpp < prev    next >
Text File  |  1997-01-03  |  9KB  |  347 lines

  1. //
  2. //  CXBindCtx.cpp
  3. //
  4. //  Copyright (C) Microsoft Corporation, 1996
  5. //
  6. //  Very basic implementation of the IBindCtx interface.  We assume that we only
  7. //  have the basic in-proc COM DLL which doesn't support the CreateBindCtx API.
  8. //  This implementation need only support the minimum set of functionality for
  9. //  our Netscape URL moniker.
  10. //
  11.  
  12. #define FAR
  13. #include "config.h"
  14.  
  15. #include <ole2.h>
  16. #include <dispatch.h>
  17. #include <wintypes.h>
  18. #include "CXBindCtx.h"
  19. #include "debug.h"
  20.  
  21. CXBindCtx::~CXBindCtx()
  22. {
  23.     LPXOBJECTPARAM pCurrentParam;
  24.     LPXOBJECTPARAM pTempParam;
  25.  
  26.     //  Useful only if our URL moniker supports BindToObject.
  27.     //  ReleaseBoundObjects();
  28.  
  29.     //  Release any registered object parameters and destroy the list.
  30.     for (pCurrentParam = m_pParamList; pCurrentParam != NULL; ) {
  31.         pTempParam = pCurrentParam;
  32.         pCurrentParam = pCurrentParam->pNextParam;
  33.         pTempParam->punkObject->Release();
  34.         CoTaskMemFree(pTempParam);
  35.     }
  36. }
  37.  
  38. //
  39. //  CXBindCtx::Lookup
  40. //
  41.  
  42. BOOL
  43. CXBindCtx::Lookup(LPOLESTR pszKey, LPXOBJECTPARAM *ppParam, BOOL fRemove)
  44. {
  45.     LPXOBJECTPARAM pPrevParam;
  46.     LPXOBJECTPARAM pCurrentParam;
  47.  
  48.     for (pPrevParam = NULL, pCurrentParam = m_pParamList; pCurrentParam != NULL;
  49.         pPrevParam = pCurrentParam, pCurrentParam = pCurrentParam->pNextParam) {
  50.         if (strcmp(pCurrentParam->key, pszKey) == 0) {
  51.             //  Remove the parameter from the list if requested.
  52.             if (fRemove) {
  53.                 if (pPrevParam == NULL)
  54.                     m_pParamList = pCurrentParam->pNextParam;
  55.                 else
  56.                     pPrevParam->pNextParam = pCurrentParam->pNextParam;
  57.             }
  58.             *ppParam = pCurrentParam;
  59.             return TRUE;
  60.         }
  61.     }
  62.  
  63.     return FALSE;
  64. }
  65.  
  66. //
  67. //  CXBindCtx::IUnknown::QueryInterface
  68. //
  69. //  Returns a pointer to the specified interface on a component to which a
  70. //  client currently holds an interface pointer.
  71. //
  72.  
  73. STDMETHODIMP
  74. CXBindCtx::QueryInterface(REFIID riid, LPVOID *ppvObj)
  75. {
  76.     HRESULT hr;
  77.     LPVOID pv;
  78.  
  79.     if (riid == IID_IUnknown || riid == IID_IBindCtx) {
  80.         pv = (LPVOID)(LPBINDCTX) this;
  81.         ++m_cRef;
  82.         hr = ResultFromScode(S_OK);
  83.     } else {
  84.         pv = NULL;
  85.         hr = ResultFromScode(E_NOINTERFACE);
  86.     }
  87.  
  88.     *ppvObj = pv;
  89.     return hr;
  90. }
  91.  
  92. //
  93. //  CXBindCtx::IUnknown::AddRef
  94. //
  95. //  Increments the reference count for the calling interface.
  96. //
  97.  
  98. STDMETHODIMP_(ULONG)
  99. CXBindCtx::AddRef(void)
  100. {
  101.     return ++m_cRef;
  102. }
  103.  
  104. //
  105. //  CXBindCtx::IUnknown::Release
  106. //
  107. //  Decrements the reference count for the calling interface on a object.  If
  108. //  the reference count on the object falls to zero, the object is freed.
  109. //
  110.  
  111. STDMETHODIMP_(ULONG)
  112. CXBindCtx::Release(void)
  113. {
  114.     if (--m_cRef != 0)
  115.         return m_cRef;
  116.  
  117.     delete this;
  118.     return 0;
  119. }
  120.  
  121. //
  122. //  CXBindCtx::IBindCtx::RegisterObjectBound
  123. //
  124. //  Calls IUnknown::AddRef on the specified object to ensure that the object
  125. //  remains active until the bind context is released.  The method stores a
  126. //  pointer to the object in the bind context's internal list of pointers.
  127. //
  128.  
  129. STDMETHODIMP
  130. CXBindCtx::RegisterObjectBound(LPUNKNOWN punk)
  131. {
  132. #pragma unused (punk)
  133.     //  Useful only if our URL moniker supports BindToObject.
  134.     return ResultFromScode(E_NOTIMPL);
  135. }
  136.  
  137. //
  138. //  CXBindCtx::IBindCtx::RevokeObjectBound
  139. //
  140. //  Releases the IUnknown pointer to the specified object and removes that
  141. //  pointer from the bind context's internal list of pointers.  This undoes a
  142. //  previous call to IBindCtx::RegisterObjectBound for the same object.
  143. //
  144.  
  145. STDMETHODIMP
  146. CXBindCtx::RevokeObjectBound(LPUNKNOWN punk)
  147. {
  148. #pragma unused (punk)
  149.     //  Useful only if our URL moniker supports BindToObject.
  150.     return ResultFromScode(E_NOTIMPL);
  151. }
  152.  
  153. //
  154. //  CXBindCtx::IBindCtx::ReleaseBoundObjects
  155. //
  156. //  Releases all pointers to all objects that were previously registered by
  157. //  calls to IBindCtx::RegisterObjectBound.
  158. //
  159.  
  160. STDMETHODIMP
  161. CXBindCtx::ReleaseBoundObjects(void)
  162. {
  163.     //  Useful only if our URL moniker supports BindToObject.
  164.     return ResultFromScode(E_NOTIMPL);
  165. }
  166.  
  167. //
  168. //  CXBindCtx::IBindCtx::SetBindOptions
  169. //
  170. //  Specifies new values for the binding parameters stored in the bind context.
  171. //  Subsequent binding operations can call IBindCtx::GetBindOptions to retrieve
  172. //  the parameters.
  173. //
  174.  
  175. STDMETHODIMP
  176. CXBindCtx::SetBindOptions(LPBIND_OPTS pbindopts)
  177. {
  178.     HRESULT hr;
  179.  
  180.     if (pbindopts->cbStruct == sizeof(m_bindopts)) {
  181.         BlockMove(pbindopts, &m_bindopts, sizeof(m_bindopts));
  182.         hr = ResultFromScode(S_OK);
  183.     } else {
  184.         //  How we handle this differs between OLE on the Mac and Win32.  For
  185.         //  now, if the size isn't a BIND_OPTS, just error out.
  186.         hr = ResultFromScode(E_INVALIDARG);
  187.     }
  188.  
  189.     return hr;
  190. }
  191.  
  192. //
  193. //  CXBindCtx::IBindCtx::GetBindOptions
  194. //
  195. //  Returns the binding options stored in this bind context.
  196. //
  197.  
  198. STDMETHODIMP
  199. CXBindCtx::GetBindOptions(LPBIND_OPTS pbindopts)
  200. {
  201.     HRESULT hr;
  202.  
  203.     if (pbindopts->cbStruct == sizeof(m_bindopts)) {
  204.         BlockMove(&m_bindopts, pbindopts, sizeof(m_bindopts));
  205.         hr = ResultFromScode(S_OK);
  206.     } else {
  207.         //  How we handle this differs between OLE on the Mac and Win32.  For
  208.         //  now, if the size isn't a BIND_OPTS, just error out.
  209.         hr = ResultFromScode(E_INVALIDARG);
  210.     }
  211.  
  212.     return hr;
  213. }
  214.  
  215. //
  216. //  CXBindCtx::IBindCtx::GetRunningObjectTable
  217. //
  218. //  Provides an interface pointer to the Running Object Table (ROT) for the
  219. //  machine on which this bind context is running.
  220. //
  221.  
  222. STDMETHODIMP
  223. CXBindCtx::GetRunningObjectTable(LPRUNNINGOBJECTTABLE *pprot)
  224. {
  225.     //  Useful only if our URL moniker supports BindToObject.
  226.     *pprot = NULL;
  227.     return ResultFromScode(E_NOTIMPL);
  228. }
  229.  
  230. //
  231. //  CXBindCtx::IBindCtx::RegisterObjectParam
  232. //
  233. //  Stores an IUnknown pointer on the specified object under the specified key
  234. //  in the bind context's string-keyed table of pointers.
  235. //
  236.  
  237. STDMETHODIMP
  238. CXBindCtx::RegisterObjectParam(LPOLESTR pszKey, LPUNKNOWN punk)
  239. {
  240.     HRESULT hr;
  241.     LPXOBJECTPARAM pParam;
  242.  
  243.     if (pszKey == NULL || punk == NULL) {
  244.         hr = ResultFromScode(E_INVALIDARG);
  245.     } else {
  246.         if (Lookup(pszKey, &pParam, FALSE)) {
  247.             //  Parameter with this name already exists.  Release its object and
  248.             //  reuse the structure.
  249.             pParam->punkObject->Release();
  250.             goto SetObjectPointer;
  251.         } else {
  252.             if ((pParam = (LPXOBJECTPARAM) CoTaskMemAlloc(sizeof(XOBJECTPARAM) +
  253.                 strlen(pszKey))) == NULL) {
  254.                 hr = ResultFromScode(E_OUTOFMEMORY);
  255.             } else {
  256.                 strcpy(pParam->key, pszKey);
  257.                 pParam->pNextParam = m_pParamList;
  258.                 m_pParamList = pParam;
  259. SetObjectPointer:
  260.                 pParam->punkObject = punk;
  261.                 punk->AddRef();
  262.                 hr = ResultFromScode(S_OK);
  263.             }
  264.         }
  265.     }
  266.  
  267.     return hr;
  268. }
  269.  
  270. //
  271. //  CXBindCtx::IBindCtx::GetObjectParam
  272. //
  273. //  Retrieves the pointer associated with the specified key in the bind
  274. //  context's string-keyed table of pointers.
  275. //
  276.  
  277. STDMETHODIMP
  278. CXBindCtx::GetObjectParam(LPOLESTR pszKey, LPUNKNOWN *ppunk)
  279. {
  280.     HRESULT hr;
  281.     LPUNKNOWN punk;
  282.     LPXOBJECTPARAM pParam;
  283.  
  284.     if (pszKey == NULL || ppunk == NULL) {
  285.         punk = NULL;
  286.         hr = ResultFromScode(E_INVALIDARG);
  287.     } else {
  288.         if (Lookup(pszKey, &pParam, FALSE)) {
  289.             punk = pParam->punkObject;
  290.             punk->AddRef();
  291.             hr = ResultFromScode(S_OK);
  292.         } else {
  293.             punk = NULL;
  294.             hr = ResultFromScode(E_FAIL);
  295.         }
  296.     }
  297.  
  298.     *ppunk = punk;
  299.     return hr;
  300. }
  301.  
  302. //
  303. //  CXBindCtx::IBindCtx::EnumObjectParam
  304. //
  305. //  Supplies a pointer to an IEnumString interface on an enumerator that can
  306. //  return the keys of the bind context's string-keyed table of pointers.
  307. //
  308.  
  309. STDMETHODIMP
  310. CXBindCtx::EnumObjectParam(LPENUMSTRING *ppenum)
  311. {
  312.     //  The default OLE32 IBindCtx does not implement this method, so there's
  313.     //  no reason for us to bother.
  314.     *ppenum = NULL;
  315.     return ResultFromScode(E_NOTIMPL);
  316. }
  317.  
  318. //
  319. //  CXBindCtx::IBindCtx::RevokeObjectParam
  320. //
  321. //  Removes the specified key and its associated pointer from the bind context's
  322. //  string-keyed table of objects. The key must have previously been inserted
  323. //  into the table with a call to IBindCtx::RegisterObjectParam.
  324. //
  325.  
  326. STDMETHODIMP
  327. CXBindCtx::RevokeObjectParam(LPOLESTR pszKey)
  328. {
  329.     HRESULT hr;
  330.     LPXOBJECTPARAM pParam;
  331.  
  332.     if (pszKey == NULL) {
  333.         hr = ResultFromScode(E_INVALIDARG);
  334.     } else {
  335.         if (Lookup(pszKey, &pParam, TRUE)) {
  336.             pParam->punkObject->Release();
  337.             CoTaskMemFree(pParam);
  338.             hr = ResultFromScode(S_OK);
  339.         } else {
  340.             //  OLE32 returns E_FAIL, not S_FALSE as the SDK documents.
  341.             hr = ResultFromScode(E_FAIL);
  342.         }
  343.     }
  344.  
  345.     return hr;
  346. }
  347.